package View.Town;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import Model.Game;
import Model.Player;
import Model.RandomEvent;
import Presenter.MainGameScreen;

/**
 * Constructs the screen for the Pub in the town
 * 
 * @author Emily
 * 
 */
public class PubView extends JPanel {

	public JButton btnStartNextRound;
	public Game g;
	public MainGameScreen gameScreen;
	public Player currentPlayer;

	/**
	 * Constructs the JComponents for the screen
	 */
	public PubView(Game g, int winnings, final MainGameScreen gameScreen) {
		this.g = g;
		this.gameScreen = gameScreen;
		setBackground(Color.BLACK);
		setLayout(null);
		setPreferredSize(new Dimension(1200, 800));

		// Logic for the random event
		Random random = new Random();
		currentPlayer = g.getPlayers()[gameScreen.getRound().getTurn().getCurrentPlayer()];
		currentPlayer.setScore(currentPlayer.getScore() + random.nextInt(200000));
		Player players[] = g.getPlayers();
		RandomEvent re = new RandomEvent();
		boolean eventOccurs = re.eventOccurs(currentPlayer, players);
		String eventMessage = "No event occurred :(";
		
		if (eventOccurs) {
			eventMessage = re.executeAndReturnEvent(g.getCurrentRound(),
					currentPlayer);
			updatePlayerInfo();
		}
		
		JLabel eventLabel = new JLabel(eventMessage);
		eventLabel.setHorizontalAlignment(SwingConstants.CENTER);
		eventLabel.setFont(new Font("Helvetica", Font.BOLD, 20));
		eventLabel.setForeground(new Color(204, 153, 0));
		eventLabel.setSize(1199, 30);
		eventLabel.setLocation(59, 330);
		add(eventLabel);

		JLabel labelPub = new JLabel("PUB");
		labelPub.setHorizontalAlignment(SwingConstants.CENTER);
		labelPub.setFont(new Font("Helvetica", Font.BOLD, 70));
		labelPub.setForeground(new Color(204, 153, 0));
		labelPub.setBounds(494, 164, 211, 97);
		add(labelPub);

		btnStartNextRound = new JButton("START NEXT TURN");

		JLabel money = new JLabel("YOUR GAMBLING WINNINGS ARE " + winnings
				+ " DOLLARS");
		money.setHorizontalAlignment(SwingConstants.CENTER);
		money.setFont(new Font("Helvetica", Font.BOLD, 20));
		money.setForeground(new Color(204, 153, 0));
		money.setSize(481, 30);
		money.setLocation(359, 300);
		add(money);

		btnStartNextRound.setFont(new Font("Helvetica", Font.BOLD, 30));
		btnStartNextRound.setBounds(408, 452, 383, 97);
		add(btnStartNextRound);
	}
	
	/**
	 * Updates the player name and money amount in the infopanel.
	 */
	public void updatePlayerInfo()
	{
		gameScreen.updatePlayer(currentPlayer.getName());
		gameScreen.updateMoney(currentPlayer.getMoney());
	}
}
